package com.cash.channel.web.controller;

import com.cash.channel.web.annotation.RestControllerMapper;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.web.model.RequestModel;
import com.cash.channel.api.util.ApiUtils;
import com.cash.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@Slf4j
@RestControllerMapper("/repay")
public class RepayController extends BaseController {

    @PostMapping("/detail")
    public Result detail(@RequestBody RequestModel requestModel) {
        try {
            return doDispatch("repayDetail" ,requestModel,  dataMap -> {
                ApiUtils.checkParamsExist(dataMap,"orderNo","period","withdrawId");
            });
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

    @PostMapping("/plan/list")
    public Result planList(@RequestBody RequestModel requestModel) {
        try {
            return doDispatch("repayPlanList" ,requestModel,  dataMap -> {
                ApiUtils.checkParamsExist(dataMap,"orderNo","withdrawId");
            });
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

    @PostMapping("/submit")
    public Result submit(@RequestBody RequestModel requestModel) {
        try {
            return doDispatch("repaySubmit" ,requestModel,  dataMap -> {
                ApiUtils.checkParamsExist(dataMap,"orderNo","userId","payType","withdrawId");
                if (1 == MapUtils.getInteger(dataMap, "payType")) {
                    ApiUtils.checkParamsExist(dataMap,"period");
                }
            });
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

    @PostMapping("/status")
    public Result status(@RequestBody RequestModel requestModel) {
        try {
            return doDispatch("repayStatus" ,requestModel,  dataMap -> {
                if (StringUtils.isEmpty(MapUtils.getString(dataMap, "extId"))) {
                    ApiUtils.checkParamsExist(dataMap,"orderNo","userId","period","withdrawId");
                } else {
                    ApiUtils.checkParamsExist(dataMap,"extId");
                }
            });
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

    @PostMapping("/calculate")
    public Result calculate(@RequestBody RequestModel requestModel) {
        try {
            return doDispatch("repayCalculate" ,requestModel,  dataMap -> {
                ApiUtils.checkParamsExist(dataMap,"withdrawId");
            });
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }
}
